tsset countrycode year, yearly
encode rrerclassification, generate(erregimecode)
replace erregimecode=0 if erregimecode==3
replace erregimecode=0 if erregimecode==1
encode erregime, generate(erregimecodeimf)
replace erregimecodeimf=0 if erregimecodeimf==3
replace erregimecodeimf=0 if erregimecodeimf==1
replace erregimecodeimf=0 if erregimecodeimf==5
generate govdeficit = -fiscalbalancepcofgdp


***MULTIPLE IMPUTATION FOR CAPITAL ACCOUNT OPENNESS AND DOMESTIC CREDIT SHARE

**examining patterns of missing values

mdesc spreadlongterminterestrates unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower erregimecode inflation governmentdebtpcofgdp domesticcreditpcofgdp capitalaccountopenness if year<2018

list country year domesticcreditpcofgdp if domesticcreditpcofgdp == ., clean noobs
list country year domesticcreditpcofgdp if capitalaccountopenness == ., clean noobs

**test regressions to see which predictors are important 

xtreg domesticcreditpcofgdp spreadlongterminterestrates unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecode##c.inflation governmentdebtpcofgdp  capitalaccountopenness i.countrycode i.year, vce(cluster countrycode)
xtreg capitalaccountopenness domesticcreditpcofgdp spreadlongterminterestrates unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecode##c.inflation governmentdebtpcofgdp  i.countrycode i.year, vce(cluster countrycode)
mi set flong
mi misstable sum spreadlongterminterestrates concentrationofpower domesticcreditpcofgdp capitalaccountopenness 
mi misstable patterns spreadlongterminterestrates concentrationofpower domesticcreditpcofgdp capitalaccountopenness 
mi misstable nested spreadlongterminterestrates concentrationofpower domesticcreditpcofgdp capitalaccountopenness

**registering values

mi register imputed domesticcreditpcofgdp capitalaccountopenness spreadlongterminterestrates concentrationofpower
mi register regular unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats erregimecode inflation governmentdebtpcofgdp

**imputation model

set seed 29390
mi impute regress domesticcreditpcofgdp = unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats i.erregimecode##c.inflation governmentdebtpcofgdp i.countrycode i.year, add(20) force
mi impute regress capitalaccountopenness = unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats i.erregimecode##c.inflation governmentdebtpcofgdp i.countrycode i.year, replace force

**diagnostics

mi xeq 0 1 5 10 15 20: summarize domesticcreditpcofgdp , d
mi xeq 0 1 5 10 15 20: summarize capitalaccountopenness , d

**averaging across 20 realizations of the missing values

mi unset
foreach var of varlist domesticcreditpcofgdp capitalaccountopenness{
bysort country year ( mi_m): egen m`var' = mean(`var')
}
keep if mi_m == 0

**Diagnostics

mdesc domesticcreditpcofgdp capitalaccountopenness mdomesticcreditpcofgdp mcapitalaccountopenness
list country year if domesticcreditpcofgdp != . & domesticcreditpcofgdp != mdomesticcreditpcofgdp
list country year if capitalaccountopenness != . & capitalaccountopenness != mcapitalaccountopenness

****WE HAVE COPIED mdomesticcreditpcofgdp AND mcapitalaccountopenness INTO OUR ORIGINAL EXCEL FILE


clear

********************************************************************************
*************APPENDIX D: Models with imputed variables**************************
********************************************************************************
 
*******************************************************************************
 **INFLATION 

**Using I, R & R classification

local inflation "spreadlongterminterestrates d.unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecode##c.L1.inflation L1.governmentdebtpcofgdp mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.inflation"

local model "dataset"

tempname memhold

postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `inflation' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecode#c.`keyvariable'])) (_b[`keyvariable']), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3])
}

postclose `memhold'
use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}

**For Figure D_1a

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Inflation") ycommon legendfrom(mu.gph)

clear

**Using IMF classification

local inflation "spreadlongterminterestrates d.unemployment realgdpgrowth currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecodeimf##c.L1.inflation L1.governmentdebtpcofgdp mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.inflation"

local model "dataset"

tempname memhold
postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `inflation' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3])
}
postclose `memhold'

use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}

**For Figure D_1b

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Inflation") ycommon legendfrom(mu.gph)

clear



****************************************************************************
**DEBT 

**Using I, R & R classification

local government_debt "spreadlongterminterestrates d.unemployment realgdpgrowth L1.inflation currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecode##c.L1.governmentdebtpcofgdp mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.governmentdebtpcofgdp"

local model "dataset"

tempname memhold
postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `government_debt' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecode#c.`keyvariable'])), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3]) 
}
postclose `memhold'

use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}
 
**For Figure D_2a

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Gov't Debt") ycommon legendfrom(mu.gph) 

clear

**Using IMF classification

local government_debt "spreadlongterminterestrates d.unemployment realgdpgrowth L1.inflation currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecodeimf##c.L1.governmentdebtpcofgdp mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.governmentdebtpcofgdp"

local model "dataset"

tempname memhold
postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `government_debt' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecodeimf#c.`keyvariable'])), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3]) 
}
postclose `memhold'

use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}
 
**For Figure D_2b

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(3)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Gov't Debt") ycommon legendfrom(mu.gph) 

clear

****************************************************************************
**DEFICIT 

**Using I, R & R classification

local government_deficit "spreadlongterminterestrates d.unemployment realgdpgrowth L1.inflation currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecode##c.L1.govdeficit mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.govdeficit"

local model "dataset"

tempname memhold
postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `government_deficit' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecode#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecode#c.`keyvariable'])), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3]) 
}
postclose `memhold'

use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}

**For Figure D_3a

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Gov't Deficit") ycommon legendfrom(mu.gph) 

clear

**Using IMF classification

local government_deficit "spreadlongterminterestrates d.unemployment realgdpgrowth L1.inflation currentaccountbalancepcofgdp election_yn centercabinetseats leftcabinetseats concentrationofpower i.erregimecodeimf##c.L1.govdeficit mdomesticcreditpcofgdp mcapitalaccountopenness i.countrycode i.year"

local keyvariable "L1.govdeficit"

local model "dataset"

tempname memhold
postfile `memhold' year b_mu se_mu b_flex se_flex b_fixed se_fixed using `model'.dta, replace
forvalues n = 1999(1)2017{
local g = `n' - 19
xtreg `government_deficit' i.year if inrange(year,`g',`n'), vce(cluster countrycode)
nlcom (_b[`keyvariable']+(_b[4.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[0.erregimecodeimf#c.`keyvariable'])) (_b[`keyvariable']+(_b[2.erregimecodeimf#c.`keyvariable'])), post
post `memhold' (`n') (_b[_nl_1]) (_se[_nl_1]) (_b[_nl_2]) (_se[_nl_2]) (_b[_nl_3]) (_se[_nl_3]) 
}
postclose `memhold'

use `model'.dta, clear
local exregimes "mu flex fixed"
foreach var of local exregimes{
gen ub`var' = b_`var' + 1.96* se_`var'
}
foreach var of local exregimes{
gen lb`var' = b_`var' - 1.96* se_`var'
}

**For Figure D_3b

twoway(rcap ubmu lbmu year, saving(mu.gph, replace) color(black) title("Monetary Union")) (connected  b_mu year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubflex lbflex year, saving(flex.gph, replace) color(black) title("Flexible Exchange Rates")) (connected  b_flex year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))
twoway(rcap ubfixed lbfixed year, saving(fixed.gph, replace) color(black) title("Fixed Peg Exchange Rates")) (connected  b_fixed year, yline(0)  xtitle("year") xlabel(1998(4)2017) ytitle("marginal effect on bond spreads") legend(label(1 "95% confidence interval") label(2 "marginal effect")))

grc1leg mu.gph flex.gph fixed.gph, title("Effect of Gov't Deficit") ycommon legendfrom(mu.gph) 

clear
